package com.gm.controller;

import com.gm.bean.Admin;
import com.gm.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {
    @Autowired
    AdminMapper adminMapper;
    @PostMapping("/admin/login")
    public String doLogin(@RequestParam("nickname") String nickname,
                          @RequestParam("password") String password,
                          HttpSession session, Model model){
        Admin admin = this.adminMapper.getAdminByName(nickname);
        if(admin != null){
            if (admin.getPassword().equals(password)){
                System.out.println("登录成功!");
                session.setAttribute("admin", admin);
                return "redirect:/main.html";
            }else {
                System.out.println("密码错误!");
                model.addAttribute("msg","密码错误!");
                return "login";
            }
        }else {
            System.out.println("用户不存在!");
            model.addAttribute("msg","用户不存在!");
            return "login";
        }

    }
}
